<template>
    <BPTable ref='tableRef' align='center' row-key='id' :columns='columns' :api='pageApi' :buttons='buttons'>
        <template #nickname="{record}">
            <a-space>
                <a-avatar v-if="record.avatar" :src="staticUrl + record.avatar" />
                <span>{{ record.nickname }}</span>
            </a-space>
        </template>
        <template #$action="{record}">
            <BTextButton type="primary" @click="openRecharge(record)">充值</BTextButton>
        </template>
    </BPTable>

    <BDrawer v-model:visible='rechargeVisible' title='充值' width='600px' :confirm='rechargesubmit'>
        <Recharge :row="currentRow" ref="rechargeformRef" />
    </BDrawer>
</template>

<script lang='ts' setup>
    import { ref } from "vue";
    import { pageApi } from '@/api/User'
    import { parseTime } from "@/utils";
    import NetImg from '@/components/NetImg.vue'

    import Recharge from "./Recharge.vue";
import { message, Modal } from "ant-design-vue";
import { useI18nStore } from "@/store/i18n";

    // import { CLIENT_SOURCE } from "@/constant";

    // CLIENT_SOURCE

    const staticUrl = import.meta.env.B_NET_STATIC_URL

    const i18n = useI18nStore()

    const tableRef = ref()

    const rechargeVisible = ref(false)
    const rechargeformRef = ref()

    const currentRow = ref<any>()


    const buttons = ref<SearchButton[]>([])

    const columns = ref<Column[]>([
        {
            dataIndex : 'nickname',
            title : '昵称',
            inSearch : true,
        },

        // {
        //     dataIndex : 'source',
        //     title : '来源',
        //     customRender({text}) {
        //         return CLIENT_SOURCE[text]
        //     }
        // },

        {
            dataIndex : 'amount',
            title : '充值余额(元)',
        },

        {
            dataIndex : 'rewardAmount',
            title : '赠送余额(元)',
        },

        {
            title : '最近登录',
            dataIndex : 'lastLoginTime',
            align : 'center',
            customRender({text}) {
                return parseTime(text)
            }
        },

        {
            title : '注册时间',
            dataIndex : 'registerTime',
            align : 'center',
            customRender({text}) {
                return parseTime(text)
            }
        },

        {
            title : i18n.t('operation'),
            dataIndex : '$action',
            align : 'center',
           
        }
    ])

    const dataRows = ref<any[]>([])


    function openRecharge(row) {
        currentRow.value = row
        rechargeVisible.value = true
    }



    async function rechargesubmit() {
        const res = await rechargeformRef.value.submit()
        if(res.success) {
            tableRef.value.reload()
            rechargeVisible.value = false
            message.success('充值成功')
        }else {
            Modal.error({
                title : res.msg
            })
        }
    }
    
</script>